package logic

import (
	"context"
	"fmt"
	"jdmall_svc/internal/svc"
	"jdmall_svc/jdmall"
	"jdmall_svc/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 购物车添加
func (l *ShopAddLogic) ShopAdd(in *jdmall.ShopAddRequest) (*jdmall.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	shop := mysql.Shop{
		UserId: int(in.UserId),
		Name:   in.Name,
		Price:  float64(in.Price),
		Stock:  int(in.Stock),
		Status: int(in.Status),
	}

	if in.Num > int64(shop.Stock) {
		logx.Error("库存不足")
		return nil, fmt.Errorf("库存不足")
	}

	mysql.DB.Debug().Create(&shop)

	return &jdmall.ShopAddResponse{
		Id: int64(shop.ID),
	}, nil
}
